//'#########################################################################
//'#                            CHANGE LOG
//'#########################################################################
//'# Author      : jianhuan.weng
//'# Auditor     : jane.gu[Double Checker]
//'# Modified    : 2009-1-12 16:00
//'# Task No     : 90033 [MYZHAOPINA注册时检查用户EMAIL是否可用BUG]
//'# Requirement : [需求人-技术部 赵永江]
//'# Purpose     : 进行用户EMAIL检测提交时增加当前时间参数
//'# Scope       : My.zhaopin.com
//'# Risk        : [修改带来的潜在风险]
//'# Approved by : [ruud.zhao]
//'########################################################################
if (typeof MZ_REG == "undefined" || !MZ_REG) var MZ_REG = {};
MZ_REG.HTML_class = {
	infoboxOk: 'yzi_td',
	infoboxWarning: 'yzi_td',
	infoboxError: 'yz_td',
	infoboxHint: 'yzi_td'
}

MZ_REG.iniForm = function(formName) {
	var s = this;
	s.objForm = document.forms[formName] || null;
	if (!s.objForm) return;
	s.objForm.allVarFormEle = new Array();
	var i, objFormEle, varFormEle;
	for (i = 0; i < s.objForm.length; i++) {
		objFormEle = s.objForm.elements[i];
		if (window[objFormEle.name] && objFormEle.name != "istest") {
			varFormEle = eval(objFormEle.name);
			varFormEle.o = objFormEle;
			varFormEle.setInfobox();
			s.objForm.allVarFormEle.push(varFormEle);
			if (varFormEle.o.getAttribute('type').toLowerCase() != 'checkbox') {
				varFormEle.o.onblur = function() {
					window[this.name].fnBlur(true);
				};
				varFormEle.o.onfocus = function() {
					window[this.name].fnFocus();
				};
			} else varFormEle.o.onclick = function() {
				window[this.name].fnBlur();
			};
		}
	}
	s.checkForm = function() {
		var s = this.objForm;
		var flag = true;
		var isajax = false;
		for (var i = 0; i < s.allVarFormEle.length; i++) {
			s.allVarFormEle[i].fnBlur(true);
			if (s.allVarFormEle[i].s != 0) flag = false;
			if (s.allVarFormEle[i].c) isajax = true;
		}
		//return flag;
		/*if (isajax) {
			MZ_REG.checkMailByAJAX(s.email, flag);
			return false;
		}*/
		return flag;
	}
}

MZ_REG.formEle = function(required, datatype, parameter, infobox, errormsg, status, combine, onlyPhone) {
	var s = this;
	s.r = required;
	s.d = datatype;
	s.p = parameter;
	s.i = infobox;
	s.e = errormsg;
	s.s = status;
	s.c = combine;
	s.o = null;
	s.onlyPhone = onlyPhone;
	switch (s.d) {
	case 'email':
		s.checkData = MZ_REG.checkEmail;
		break;
	case 'password':
		s.checkData = MZ_REG.checkPassword;
		break;
	case 'mirror':
		s.checkData = MZ_REG.checkMirror;
		break;
	case 'checkbox':
		s.checkData = MZ_REG.checkCheckbox;
		break;
	case 'text':
		s.checkData = MZ_REG.checkText;
		break;
	case 'verifyCode':
		s.checkData = MZ_REG.checkVerifycode;
		break;
	case 'mailverifyCode':
		s.checkData = MZ_REG.checkMailverifyCode;
		break;
	case '_mailverifyCode':
		s.checkData = MZ_REG._checkMailverifyCode;
		break;
	default:
		break;
	}

	s.fnBlur = function(f) {
		var s = this;
		if (s.o) {
			var status = s.checkData(s.o, s.onlyPhone);
			s.setStatus(status);
			s.setInfobox();
			if(s.o.name == "email" && status == 0 && f) MZ_REG.checkMailByAJAX(s.o);
			if(s.o.name == "loginname" && status == 0 && f) MZ_REG.checkMailByAJAX(s.o);
			if(s.o.name=='email'&&status!==0){
				if($('.verifyArea').is(':visible')) verifyFn.hide();
			}
			

		} else return;
	};
	s.fnFocus = function() {
		var s = this;
		if (s.o) {
			var infobox = s.getInfobox();
			var status = s.getStatus();
			var flagAB = null;
			if (infobox.parentNode.parentNode.tagName.toLowerCase() == "dl") flagAB = infobox.parentNode.parentNode;
			if (s.getMsg(0) != null) infobox.innerHTML = s.getMsg(0);
			else infobox.innerHTML = "";
			if (flagAB !== null && flagAB.nodeType == 1) flagAB.className = "";
			else infobox.className = MZ_REG.HTML_class.infoboxWarning;
		} else return;
	};
	s.setInfobox = function() {
		var s = this;
		if (s.o) {
			var infobox = s.getInfobox();
			var status = s.getStatus();
			var flagAB = null;

			if (infobox.parentNode.parentNode.tagName.toLowerCase() == "dl") flagAB = infobox.parentNode.parentNode;
			if (typeof status == 'undefined' || status == '' || status == 0) {
				if (s.getMsg(0) != null) infobox.innerHTML = s.getMsg(0);
				else infobox.innerHTML = '';
				if (flagAB !== null && flagAB.nodeType == 1) flagAB.className = "";
				else infobox.className = MZ_REG.HTML_class.infoboxHint;
			} else if (status == -1) {
				if (s.isRequired()) {
					if (s.getMsg(2) != null) infobox.innerHTML = s.getMsg(2);
					else infobox.innerHTML = '';
					if (flagAB !== null && flagAB.nodeType == 1) flagAB.className = "error";
					else infobox.className = MZ_REG.HTML_class.infoboxError;
				} else if (!s.isRequired()) {
					if (s.getMsg(0) != null) infobox.innerHTML = s.getMsg(0);
					else infobox.innerHTML = '';
					if (flagAB !== null && flagAB.nodeType == 1) flagAB.className = "";
					else infobox.className = MZ_REG.HTML_class.infoboxHint;
				}
			} else if (status == 1) {
				if (s.getMsg(1) != null) infobox.innerHTML = s.getMsg(1);
				else infobox.innerHTML = '';
				if (flagAB !== null && flagAB.nodeType == 1) flagAB.className = "";
				else infobox.className = MZ_REG.HTML_class.infoboxOk;
			} else if (status > 1) {
				if (s.getMsg(status) != null) {
					infobox.innerHTML = s.getMsg(status);
					if (flagAB !== null && flagAB.nodeType == 1) flagAB.className = "error";
					else infobox.className = MZ_REG.HTML_class.infoboxError;
				}
			}
		} else return;
	};
	s.isRequired = function() {
		var s = this;
		return s.r;
	};
	s.getDatatype = function() {
		var s = this;
		return s.d;
	};
	s.getInfobox = function() {
		var s = this;
		if (document.getElementById(s.i)) return document.getElementById(s.i);
		else return null;
	};
	s.getMsg = function(errorCode) {
		var s = this;
		if (s.e[errorCode]) return s.e[errorCode];
		else return null;
	};
	s.getStatus = function() {
		var s = this;
		return s.s;
	};
	s.setStatus = function(v) {
		var s = this;
		s.s = v;
	}
}

MZ_REG.checkVerifycode = function() {
	var status = 0;	
	if(MZ_REG.checkMobile(window['email'].o.value)!==0){
		return status;
	}else{
		var _v=$("#verifyCode").val().trim();
		
	    if(_v==''){
	        return 2;
	    }

	    
	    if(!/^[0-9]{6}$/g.test(_v)){
	    	return 6;
	    }
		
		return status;
	}
}

MZ_REG.checkMailverifyCode = function(obj) {
	var status = 0;	
	if(MZ_REG.checkMobile(window['email'].o.value)==0||needflag1){
		return status;
	}else{
		var _v=obj.value.trim();;
		
	    if(_v==''){
	        return 2;
	    }

	    if(!/^[a-zA-Z0-9]{1,4}$/g.test(_v)){
	    	return 6;
	    }
		
		return status;
	}
}


MZ_REG._checkMailverifyCode = function(obj) {
	var status = 0;	
	if(needflag2){
		return status;
	}else{
		var _v=obj.value;
		
	    if(_v==''){
	        return 2;
	    }

	    if(!/^[a-zA-Z0-9]{1,4}$/g.test(_v)){
	    	return 6;
	    }
		
		return status;
	}
}



MZ_REG.checkText = function(obj) {
	var str = obj.value;
	if (str.trim() == '') return - 1;
	return 0;
}

MZ_REG.checkEmail = function(obj, onlyPhone) {
	var str = obj.value.trim();
	if (str == '') return -1;
	if(onlyPhone){
		var phNumStatus = MZ_REG.checkMobile(str);
		if(phNumStatus == 0){
			//为注册分支
			if(obj.id==="email"){
				needflag1=!0;
				verifyFn.hide1();
			}
			return 0;
		}else{
			return phNumStatus;
		}
	}else{
		if (str.length > 100) return 3;
		str = str.toLowerCase();
		var strSuffix = "cc|com|edu|gov|int|net|org|biz|info|pro|name|coop|al|dz|af|ar|ae|aw|om|az|eg|et|ie|ee|ad|ao|ai|ag|at|au|mo|bb|pg|bs|pk|py|ps|bh|pa|br|by|bm|bg|mp|bj|be|is|pr|ba|pl|bo|bz|bw|bt|bf|bi|bv|kp|gq|dk|de|tl|tp|tg|dm|do|ru|ec|er|fr|fo|pf|gf|tf|va|ph|fj|fi|cv|fk|gm|cg|cd|co|cr|gg|gd|gl|ge|cu|gp|gu|gy|kz|ht|kr|nl|an|hm|hn|ki|dj|kg|gn|gw|ca|gh|ga|kh|cz|zw|cm|qa|ky|km|ci|kw|cc|hr|ke|ck|lv|ls|la|lb|lt|lr|ly|li|re|lu|rw|ro|mg|im|mv|mt|mw|my|ml|mk|mh|mq|yt|mu|mr|us|um|as|vi|mn|ms|bd|pe|fm|mm|md|ma|mc|mz|mx|nr|np|ni|ne|ng|nu|no|nf|na|za|aq|gs|eu|pw|pn|pt|jp|se|ch|sv|ws|yu|sl|sn|cy|sc|sa|cx|st|sh|kn|lc|sm|pm|vc|lk|sk|si|sj|sz|sd|sr|sb|so|tj|tw|th|tz|to|tc|tt|tn|tv|tr|tm|tk|wf|vu|gt|ve|bn|ug|ua|uy|uz|es|eh|gr|hk|sg|nc|nz|hu|sy|jm|am|ac|ye|iq|ir|il|it|in|id|uk|vg|io|jo|vn|zm|je|td|gi|cl|cf|cn";
		var regu = "^[a-z0-9][_a-z0-9\-]*([\.][_a-z0-9\-]+)*@([a-z0-9\-_]+[\.])+(" + strSuffix + ")$";
		var re = new RegExp(regu);
		var mStatus = MZ_REG.checkMobile(str);

		//为手机号
		if (mStatus == 0) {
			//为注册分支
			if(obj.id==="email"){
				//console.log("为手机号且为注册");
				needflag1=!0;
				verifyFn.hide1();
			}

			return 0;
		} else {
			//为注册分支
			/*if(obj.id==="email"){
			 //console.log("为非手机号且为注册");
			 if(!needflag1) verifyFn.show1();
			 }*/

			if (re.test(str)) {
				if (str.indexOf('http://ts.zhaopin.com/jump/js/@yahoo.cn') > 0 || str.indexOf('http://ts.zhaopin.com/jump/js/@yahoo.com.cn') > 0) return 7;//chris.cai
				return 0
			}
			return mStatus || 4;
		}
	}
}

MZ_REG.checkMobile = function(str) {	 
	var reMobile = /^1[3|4|5|7|8]\d{9}$/;
	if (reMobile.test(str)) {				
		return 0;
	}
	return 4;	
}

MZ_REG.checkPassword = function(obj) {
	var str = obj.value;
	var patn = /^[a-zA-Z0-9_]{6,20}$/;
	if (str.trim() == '') return - 1;
	else if (!patn.test(str)) return 3;
	return 0;
}

MZ_REG.checkMirror = function(obj) {
	var v1 = obj.value,
	v2;
	if (v1.trim() == '') return - 1;
	if (!MZ_REG.getAttrValueByName(obj, "sameas")) return 0;
	v2 = obj.form.elements[MZ_REG.getAttrValueByName(obj, "sameas")].value;
	if (v1 == v2) return 0;
	return 3;
}

MZ_REG.checkCheckbox = function(obj) {
	var flagChecked = obj.checked;
	if (flagChecked) return 0;
	else return 2;
}

MZ_REG.getAttrValueByName = function(obj, str) {
	function getAttrName(str) {
		var s = str.split("=");
		return s[0];
	}
	function getAttrValue(str) {
		var s = str.split("=");
		return s[1];
	}
	var para;
	if (obj.name) {
		if (eval(obj.name).p) para = eval(obj.name).p;
		else return;
	} else return;
	var s = para.split(",");
	for (var i = 0; i < s.length; i++) {
		if (getAttrName(s[i]) == str) {
			if (getAttrValue(s[i])) return getAttrValue(s[i]);
			else return;
		}
	}
	return;
}

String.prototype.trim = function() {
	var x = this;	
    x = x.replace(/^\s*|\s*$/g, "");	
	return x;
}

MZ_REG.checkMailByAJAX = function(mailInput, f) {
	var varInput = window[mailInput.name] || null;
	var status = 2;
	if (varInput) {
		status = varInput.checkData(varInput.o);
	}
	if (status < 2 && status != -1) {
		if (varInput) {
			var infobox = window[mailInput.name].getInfobox();
			var flagAB = null;
			if (infobox.parentNode.parentNode.tagName.toLowerCase() == "dl") flagAB = infobox.parentNode.parentNode;
			// var t=infobox;
			infobox.innerHTML = '';
			if (flagAB !== null && flagAB.nodeType == 1) flagAB.className = "";
			else infobox.className = MZ_REG.HTML_class.infoboxWarning;
		}
		mailInput.form['istest'].value = 1;
		
		var data = 'registerName=' + ua(mailInput.value);
	
		switch(varInput.o.name){
			case 'email' :
			var _handle = MZ_REG.handleCheckMailResponse;
			break;
			case 'loginname':
			var _handle = MZ_REG.handleCheckMailResponse2;
			break;
		}
		// alert(data);
		$.ajax({
			url:"/home/login/ajaxphone",
			type:"get",
			dataType:"jsonp",
			data:data,
			success:function(data){

				alert(111);
				// if(re==1)
				// {
				// infobox.innerHTML='可以使用';
				// }else
				// {
				// infobox.innerHTML='手机号已注册';	
				// }
			}
		});

		
	} else return;
}

MZ_REG.handleCheckMailResponse = function (r,s,xhr) {
	var ErrorCode=r.ErrorCode,MessageText=r.MessageText,mailVarName='email';

	switch(ErrorCode){
		//验证OK
		case 0:
		ErrorCode=0;
		break;
		//账户已存在
		case 1:
		ErrorCode=5;
		break;
		//操作频繁
		case 2:
		ErrorCode=8;
		break;
	}

	window[mailVarName].setStatus(ErrorCode);
    window[mailVarName].setInfobox();

    if(ErrorCode==500){
    	var infobox = window[mailVarName].getInfobox();
            infobox.innerHTML = '操作失败！请再试一次！';
            infobox.className = MZ_REG.HTML_class.infoboxError;
    }

    if(ErrorCode==0){
		if (MZ_REG.checkMobile(window[mailVarName].o.value)==0) {verifyFn.show();}
			else {verifyFn.hide();}
    }else{
    	verifyFn.hide();
    }
}


MZ_REG.handleCheckMailResponse2 = function (r,s,xhr) {

	var ErrorCode=r.ErrorCode,MessageText=r.MessageText,mailVarName='loginname';

	switch(ErrorCode){
		case 0:
		ErrorCode=5;
		break;
		case 1:
		ErrorCode=0;
		break;
		case 2:
		ErrorCode=8;
		break;
	}

	window[mailVarName].setStatus(ErrorCode);
    window[mailVarName].setInfobox();

    if(ErrorCode==500){
    	var infobox = window[mailVarName].getInfobox();
            infobox.innerHTML = '操作失败！请再试一次！';
            infobox.className = MZ_REG.HTML_class.infoboxError;
    }





    /*var tem = success ;
    success = responseText;
    responseText = tem + '';    
    var mailVarName = 'loginname';
    if (window[mailVarName]) {
        if (success) {
            var errorIndex = responseText.charAt(responseText.length - 1);
            if(errorIndex==5||errorIndex==6) errorIndex=1;
            else if(errorIndex==1) errorIndex=5;
            if (!isNaN(errorIndex)) {
                window[mailVarName].setStatus(errorIndex);
                window[mailVarName].setInfobox();
            }
        }
        else {
            window[mailVarName].setStatus(99);
            var infobox = window[mailVarName].getInfobox();
            infobox.innerHTML = '操作失败！请再试一次！';
            infobox.className = MZ_REG.HTML_class.infoboxError;
        }
    }*/
}











